package handlers

import (
	"deliv-manage/cache"
	"deliv-manage/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func PersonAdd(c *gin.Context) {
	personNo := c.PostForm("person_no")
	personName := c.PostForm("person_name")
	personTel := c.PostForm("person_tel")
	personArea := c.PostForm("person_area")
	if personNo == "" || personName == "" || personTel == "" || personArea == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	person := models.Person{
		PersonNo:   personNo,
		PersonName: personName,
		PersonTel:  personTel,
		PersonArea: personArea,
	}
	err := models.DB.Create(&person).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	orderNo := c.PostForm("order_no")
	orderArea := c.PostForm("order_area")
	orderStatus := c.PostForm("order_status")
	pid, _ := strconv.Atoi(c.PostForm("pid"))

	if orderNo == "" || orderArea == "" || orderStatus == "" || pid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	order := models.Order{
		OrderNo:     orderNo,
		OrderArea:   orderArea,
		OrderStatus: orderStatus,
		Pid:         pid,
	}
	err := models.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func PersonOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))

	offset := (page - 1) * size

	var count int64
	models.DB.Model(&models.Order{}).Count(&count)

	/*
			SELECT
		  orders.id AS OrderId,
		  people.person_no AS PersonNo,
		  people.person_name AS PersonName,
		  people.person_tel AS PersonTel,
		  people.person_area AS PersonArea,
		  orders.order_no AS OrderNo,
		  orders.order_area AS OrderArea,
		  orders.order_status AS OrderStatus
		FROM
		  orders
		  LEFT JOIN people ON orders.pid = people.id
	*/

	var personOrder []models.PersonOrder
	redis_key := "list_personOrder" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderId",
				"people.person_no AS PersonNo",
				"people.person_name AS PersonName",
				"people.person_tel AS PersonTel",
				"people.person_area AS PersonArea",
				"orders.order_no AS OrderNo",
				"orders.order_area AS OrderArea",
				"orders.order_status AS OrderStatus",
			).
			Joins("LEFT JOIN people ON orders.pid = people.id").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&personOrder)
		jsonData, _ := json.Marshal(personOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &personOrder)
	}

	c.JSON(200, gin.H{
		"code":        200,
		"msg":         "查询成功",
		"count":       count,
		"personOrder": personOrder,
	})
	return
}
func PersonOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
			SELECT
		  orders.id AS OrderId,
		  people.person_no AS PersonNo,
		  people.person_name AS PersonName,
		  people.person_tel AS PersonTel,
		  people.person_area AS PersonArea,
		  orders.order_no AS OrderNo,
		  orders.order_area AS OrderArea,
		  orders.order_status AS OrderStatus
		FROM
		  orders
		  LEFT JOIN people ON orders.pid = people.id
	*/

	var personOrder models.PersonOrder
	redis_key := "detail_personOrder" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderId",
				"people.person_no AS PersonNo",
				"people.person_name AS PersonName",
				"people.person_tel AS PersonTel",
				"people.person_area AS PersonArea",
				"orders.order_no AS OrderNo",
				"orders.order_area AS OrderArea",
				"orders.order_status AS OrderStatus",
			).
			Joins("LEFT JOIN people ON orders.pid = people.id").
			Where("orders.id = ?", id).
			Debug().
			Find(&personOrder)
		jsonData, _ := json.Marshal(personOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &personOrder)
	}

	c.JSON(200, gin.H{
		"code":        200,
		"msg":         "查询成功",
		"personOrder": personOrder,
	})
	return
}
